package com.example.lt.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.lt.authority.Authority;
import com.example.lt.constant.MessageConstants;
import com.example.lt.entity.video.Type;
import com.example.lt.entity.queryPage.BasePage;
import com.example.lt.service.video.TypeService;
import com.example.lt.util.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 小涛Tao
 * @Date: 2024/08/04/8:33
 * version 1.0 注释：
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/type")
public class AdminTypeController {

    private final TypeService typeService;

    /**
     * 根据id获取分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Authority("admin:type:get")
    public Result get(@PathVariable Long id) {
        log.info("获取id为 {} 的分类", id);
        return Result.ok().data(typeService.getById(id));
    }

    /**
     * 分页查询分类
     * @param basePage
     * @return
     */
    @GetMapping("/page")
    @Authority("admin:type:page")
    public Result page(BasePage basePage) {
        log.info("分类分页查询，第{}页{}条数据", basePage.getPage(), basePage.getLimit());
        final IPage page = typeService.page(basePage.page(), null);
        return Result.ok().data(page.getRecords()).count(page.getTotal());
    }


    /**
     * 管理员添加分类
     * @param type
     * @return
     */
    @PostMapping
    @Authority("admin:type:add")
    public Result add(@RequestBody @Validated Type type) {
        log.info("添加分类：{}", type.getName());
        // 查询这个分类是否已经存在
        final int count = typeService.count(new LambdaQueryWrapper<Type>()
                .eq(Type::getName, type.getName())
                .ne(Type::getId, type.getId()));
        if (count == 1) return Result.error().message(MessageConstants.TYPE_EXIST);
        typeService.save(type);
        return Result.ok().message(MessageConstants.ADD_SUCCESS);
    }

    /**
     * 修改分类信息
     * @param type
     * @return
     */
    @PutMapping
    @Authority("admin:type:update")
    public Result update(@RequestBody @Validated Type type) {
        log.info("修改分类信息：{}", type.getId());
        final int count = typeService.count(new LambdaQueryWrapper<Type>()
                .eq(Type::getName, type.getName())
                .ne(Type::getId, type.getId()));
        if (count == 1) return Result.error().message(MessageConstants.TYPE_EXIST);
        typeService.updateById(type);
        return Result.ok().message(MessageConstants.UPDATE_SUCCESS);
    }

    /**
     * 管理端删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Authority("admin:type:delete")
    public Result delete(@PathVariable Long id) {
        log.info("删除id为{}的分类", id);
        typeService.removeById(id);
        return Result.ok().message(MessageConstants.DELETE_SUCCESS);
    }

}
